package web.java.a_BeanUtils;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import web.java.z_Utils.WebUtil;

public class RegisterServlet extends HttpServlet {
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		//接收表单数据，并封装入对象中
		String userName=request.getParameter("username");
		String password=request.getParameter("password");
		User user=new User();
		user.setUsername(userName);
		user.setPassword(password);
		System.out.println(user);
		//使用BeanUtils组件
		Map<String, String[]> map=request.getParameterMap();
		Set<Entry<String,String[]>> set=map.entrySet();
		for(Entry<String, String[]> entry:set){
			System.out.println(entry.getKey()+":"+entry.getValue()[0]);
		}
		User user1=new User();
		try {
			BeanUtils.populate(user1,map);
			System.out.println(user1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//将处理过程封装入一个工具类中
		User user3=WebUtil.encapsulate(request, User.class);
		System.out.println(user3);
	}

}
